package evil.wolf.panels;

import evil.wolf.action.Action;
import evil.wolf.business.BusinessNhap;
import evil.wolf.excute.query.ExcuteQuery;
import evil.wolf.gui.MainUI;
import evil.wolf.gui.UINhap;
import evil.wolf.objects.ObjectNhapXuat;



import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneLayout;
import javax.swing.plaf.basic.BasicSplitPaneDivider;
import javax.swing.table.AbstractTableModel;

@SuppressWarnings("serial")
public class ListNhapUI extends JPanel implements MouseListener{
	
	// Static fields.
	MainUI mainUI; 
	DateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy");
	int colCount;
	int index[];
	JPopupMenu menu = new JPopupMenu("Popup");
	ObjectNhapXuat objectNhap = new ObjectNhapXuat();
	String action = null;
	
	public List<ObjectNhapXuat> arrayLaterials;
	public JTable dataTableNhap;
	public JTable dataTableCTNhap;
	public NewTableModel dataModel;
	ListCTNhapXuatUI listCTNX = new ListCTNhapXuatUI(mainUI);
	// Constructors.
	public ListNhapUI(MainUI mainUI) {
		
		super(new BorderLayout());
		this.mainUI = mainUI;
		
		dataModel = new NewTableModel();
		
		dataTableNhap = new JTable(dataModel);
		dataTableNhap.addMouseListener(this);
		dataTableNhap.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		JScrollPane scrollpaneN = new JScrollPane(dataTableNhap);
		
		dataTableCTNhap = new JTable(listCTNX.dataModel);
		dataTableCTNhap.addMouseListener(this);
		dataTableCTNhap.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		JScrollPane scrollpanectn = new JScrollPane(dataTableCTNhap);

		JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);

		splitPane.setTopComponent(scrollpaneN);	
		splitPane.setBottomComponent(scrollpanectn);
		splitPane.setOneTouchExpandable(true);
		splitPane.setDividerLocation(300);
		
		BasicSplitPaneDivider divider = (BasicSplitPaneDivider) splitPane.getComponent(0);
		divider.setBackground(Color.cyan);
		divider.setBorder(null);
		splitPane.setDividerSize(10);

		add(splitPane);
		
	    JMenuItem item = new JMenuItem("Edit selected row");
	  
	    item.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				System.out.println("Menu item Test1Edit this row");
			}
	    });
	    
	    menu.add(item);
	    item = new JMenuItem("Delete selected row");
	    item.addActionListener(new ActionListener() {
	    	
	    	public void actionPerformed(ActionEvent e) {
	    	  
	    		if(JOptionPane.showConfirmDialog(null, "Do you really want to delete this row?", "CONFIRM", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION){
					int row = getSelectedRow();
					ExcuteQuery doAction = new ExcuteQuery();
					if(doAction.deleteNhap((String)dataTableNhap.getValueAt(getSelectedRow(), 0) , (String)dataTableNhap.getValueAt(getSelectedRow(), 2), (String)dataTableNhap.getValueAt(getSelectedRow(), 4))) {
						
						arrayLaterials.remove(row);
						dataModel.fireTableDataChanged();
					}
	    	 }
	      }
	    });
	    
	    menu.add(item);
	    dataModel.fireTableDataChanged();
	}
	

	public int getSelectedRow() {
		index  = dataTableNhap.getSelectedRows();
		return index[0];
	}
	
	public String getAction(){
		return action;
	}
	
	public void setAction(String action){
		 this.action = action;
	}
	
	@Override
	public void mouseClicked(MouseEvent me) {
		
		if(me.getClickCount() == 2 && !me.isConsumed()) {
			me.consume();
			setAction(Action.ACTION_UPDATE);
			
			objectNhap.setsoct((String) dataTableNhap.getValueAt(getSelectedRow(), 0));
//			Date date = (Date) dataTable.getValueAt(getSelectedRow(), 1);
//			String dateStr = dateFormatter.format(date);
//			try {
//				date = (Date)dateFormatter.parse(dateStr);
//			} catch (ParseException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
			objectNhap.setngayct((String)dataTableNhap.getValueAt(getSelectedRow(), 1));
			objectNhap.setmakh((String) dataTableNhap.getValueAt(getSelectedRow(), 2));
//			objectNhap.setdg((String) dataTable.getValueAt(getSelectedRow(), 3));
//			objectNhap.setloaict((String) dataTable.getValueAt(getSelectedRow(), 4));
			UINhap uiNhap = new UINhap(mainUI, this, objectNhap);
			uiNhap.setTextUI(objectNhap);
			uiNhap.setVisible(true);
		}
	}
	
	@Override
	public void mouseEntered(MouseEvent e) {
		
	}
	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mousePressed(MouseEvent ev) {
		if (ev.isPopupTrigger() && this.getSelectedRow() != -1 ) {
	          menu.show(ev.getComponent(), ev.getX(), ev.getY());
	        }
		
	}
	@Override
	public void mouseReleased(MouseEvent ev) {
		if (ev.isPopupTrigger() && this.getSelectedRow() != -1 ) {
	          menu.show(ev.getComponent(), ev.getX(), ev.getY());
	    }
	}
	
public class NewTableModel extends AbstractTableModel {
	
		BusinessNhap BusNhap = new BusinessNhap();
		
		// constructor 
		public NewTableModel() {
			arrayLaterials  = BusNhap.getListRecords();
		}
		
		@Override
		public String getColumnName(int i) {
			
		    return BusNhap.getName()[i];
		}
		
		@Override
		public int getColumnCount()	{
			return BusNhap.getColumn();
		}
		
		@Override
		public int getRowCount() {
		
			return arrayLaterials.size();
		}
		
		@Override
		public Object getValueAt(int row, int col) {
			ObjectNhapXuat objectNhap = (ObjectNhapXuat)arrayLaterials.get(row);
					
			switch (col) {
			case 0: return objectNhap.getsoct();
			case 1: return objectNhap.getngayct();
			case 2: return objectNhap.getmakh();
//			case 3: return objectNhap.getdg();
//			case 4: return objectNhap.getloaict();
			default: return null;
			}
		}
	}
}